/*
Copyright Nicolas Dessart 2010 Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef H_WORKSPACE_MANAGER
#define H_WORKSPACE_MANAGER

#include <map>
#include <utility>
#include <deque>
#include <stack>
#include <string>
#include <boost/shared_ptr.hpp>

#include "workspace.h"
#include "stdworkspace.h"

#define PWorkSp boost::shared_ptr<WorkSp<T> >

template <typename T>
class WorkSpManager
{
public:
    static PWorkSp Get()
    {
        return m_stack.top();
    }

    PWorkSp Workspace()
    {
        return m_stack.top();
    }

    static PWorkSp Pop()
    {
		PWorkSp ret;
        if (m_stack.size() != 0) 
		{
			m_stack.pop();
			ret = m_stack.top();
		}
		else
		{
			ret = PStdWks;
		}
		return ret;
    }

    static PWorkSp PushNew(PWorkSp tmp)
    {
        if (tmp)
        {
            m_stack.push(tmp);
        }
        return m_stack.top();
    }

    static PWorkSp Push()
    {
        if (m_stack.top())
        {
            PWorkSp tmp(new WorkSp<T>(*m_stack.top()));
            m_stack.push(tmp);
        }
        return m_stack.top();
    }

protected:
    WorkSpManager();
    virtual ~WorkSpManager();

	static PWorkSp PStdWks;
    static std::stack<PWorkSp > m_stack;

};

template<typename T>
PWorkSp WorkSpManager<T>::PStdWks = PWorkSp(new stdworkspace<T>);

template<typename T>
std::stack<PWorkSp > WorkSpManager<T>::m_stack = std::stack<PWorkSp >(std::deque<PWorkSp> (1,WorkSpManager<T>::PStdWks) );

#undef PWorkSp

#endif // H_WORKSPACE_MANAGER
